package microcredit.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import microcredit.model.domain.TrainingStatus;
import microcredit.model.util.ModelUtils;

/**
 * Entity implementation class for Entity: MemberToTrainingType
 *
 */
@Entity
public class MemberToTrainingType implements Serializable {

	public static final String STATUS = "status";
	public static final String DATE = "date";
	public static final String NOTES = "notes";
	
	
	private static final long serialVersionUID = 1L;

	public MemberToTrainingType() {
		super();
	}
	
	@Id
	@Column(name="MEMBER_ID", updatable=false, insertable=false)
	private Long memberID;
	
	@Id
	@Column(name="TRAININGTYPE_ID", updatable=false, insertable=false)
	private Long trainingTypeID;
	
	@Id
	@Column(name="SEQUENCE_ID")
	private Long sequenceID;
	
	private Member member;
	private TrainingType trainingType;
	
	@Temporal(TemporalType.DATE)
	private Date date;
	
	@Enumerated(EnumType.STRING)
	@Column(length=1)
	private TrainingStatus status;
	
	@Column(length=5000)
	private String notes;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date insertStamp;
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date updateStamp;
	
	@PrePersist
	public void prePersist() throws ValidationException {
		if (getInsertStamp() == null) {
			setInsertStamp(ModelUtils.createTimestamp());
		}
		setUpdateStamp(ModelUtils.createTimestamp());
		validate();
	}
	
	@PreUpdate
	public void preUpdate() throws ValidationException {
		setUpdateStamp(ModelUtils.createTimestamp());
		validate();
	}
	
	public void validate() throws ValidationException {
		if (status == null) {
			throw new ValidationException("Status must be defined.", STATUS);
		}
		if (date == null) {
			throw new ValidationException("The date must be defined.", DATE);
		}
		
		//TODO a lot more validations necessary
	}
	
	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public Long getSequenceID() {
		return sequenceID;
	}
	public void setSequenceID(Long sequenceID) {
		this.sequenceID = sequenceID;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public TrainingType getTrainingType() {
		return trainingType;
	}
	public void setTrainingType(TrainingType trainingType) {
		this.trainingType = trainingType;
	}
	public Long getMemberID() {
		return memberID;
	}
	public Long getTrainingTypeID() {
		return trainingTypeID;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public TrainingStatus getStatus() {
		return status;
	}
	public void setStatus(TrainingStatus status) {
		this.status = status;
	}
	public Date getInsertStamp() {
		return insertStamp;
	}
	public void setInsertStamp(Date insertStamp) {
		this.insertStamp = insertStamp;
	}
	public Date getUpdateStamp() {
		return updateStamp;
	}
	public void setUpdateStamp(Date updateStamp) {
		this.updateStamp = updateStamp;
	}
	@Override
	public String toString() {
		if (sequenceID == null) {
			return "Member training assignment ";
		} else {
			return "Member training assignment ["+member.toString()+" -> "+trainingType.toString()+"]";
		}
	}
}
